package com.tbit.uqbike.service.base.impl;


import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.BatteryModel;
import com.tbit.uqbike.object.pojo.BatteryModelDetail;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.BatteryModelDetailService;
import com.tbit.uqbike.service.base.BatteryModelService;
import com.tbit.uqbike.service.business.AccountService;
import com.tbit.uqbike.webmanager.dao.core.BatteryModelDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("batteryModelDetailService")
public class BatteryModelDetailServiceImpl implements BatteryModelDetailService {
    @Autowired
    private BatteryModelDetailDao batteryModelDetailDao;
    @Autowired
    private BatteryModelService batteryModelService;
    @Autowired
    private AccountService accountService;

    @Override
    public void insert(BatteryModelDetail batteryModelDetail) {

        batteryModelDetailDao.insert(batteryModelDetail);
    }

    @Override
    public void del(Integer batteryModelDetailId) {

        batteryModelDetailDao.del(batteryModelDetailId);
    }

    @Override
    public List<BatteryModelDetail> getByBatteryModelId(Integer batteryModelId, Integer rowCount, Integer pageNO) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("batteryModelId", batteryModelId);
        params.put("pageSize", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);

        return batteryModelDetailDao.getByBatteryModelId(params);
    }

    @Override
    public BatteryModelDetail getByMachineId(Integer machineId, Float vol) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineId", machineId);
        params.put("vol", vol);

        return batteryModelDetailDao.getByMachineId(params);
    }

    @Override
    public Integer getTotal(Integer batteryModelId) {

        return batteryModelDetailDao.getTotal(batteryModelId);
    }

    @Override
    public void addList(List<BatteryModelDetail> batteryModelDetail) {

        batteryModelDetailDao.insertList(batteryModelDetail);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void editList(LoginInfo loginInfo, Integer batteryModelId,
                         String name, String remark,
                         String maxVols, String minVols,
                         String batteryPers, String milages) {

        List<BatteryModelDetail> batteryModelDetails = new ArrayList<>();
        //权限验证
        BatteryModel batteryModel = batteryModelService.getByBatteryModelId(batteryModelId);
        if (batteryModel == null) {
            throw new BaseException(SystemCodeEnum.BATTERY_NOT_EXIST);
        }
        if (!(loginInfo.type.equals(UserConstant.USER_ADMIN) || loginInfo.type.equals(UserConstant.USER_BRAND))) {
            List<Integer> accountIds = accountService.getAllOperateAccountId(loginInfo);
            if (!accountIds.contains(batteryModel.getAccountId())) {
                throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
            }
        }
        if (batteryModel.createByAdmin() && loginInfo.roleNotAdmin()) {
            throw new BaseException("非超级管理员不可修改系统模板");
        }
        if (batteryModel.createByBrand() && loginInfo.roleLowerThanBrand()) {
            throw new BaseException("无权修改:品牌创建的电池方案只有超级管理员和品牌修改");
        }
        //删除原有电池方案详情
        batteryModelDetailDao.delByBatteryModelId(batteryModelId);
        //添加新的
        String[] maxVolB = maxVols.split(",");
        String[] minVolB = minVols.split(",");
        String[] batteryPerB = batteryPers.split(",");
        String[] milageB = milages.split(",");
        if (maxVolB.length != minVolB.length
                || maxVolB.length != batteryPerB.length
                || maxVolB.length != milageB.length) {
            throw new BaseException(SystemCodeEnum.PARAM_CHECK_FAIL);
        }
        Double[][] voltageArr = new Double[maxVolB.length][2];
        for (int i = 0; i < minVolB.length; i++) {
            voltageArr[i][0] = Double.valueOf(minVolB[i]);
            voltageArr[i][1] = Double.valueOf(maxVolB[i]);
        }
        if (voltageArr[minVolB.length - 1][0] > voltageArr[minVolB.length - 1][1]) {
            throw new BaseException("最后一行最小电压不能大于最大电压值");
        }
        for (int k = 0; k < voltageArr.length - 1; k++) {
            if (voltageArr[k][0] > voltageArr[k][1]) {
                throw new BaseException("第" + (k + 1) + "行最小电压值不能大于最大电压值");
            }
            if (voltageArr[k][1] > voltageArr[k + 1][0]) {
                throw new BaseException("第" + (k + 1) + "行最大电压值与第" + (k + 2) + "行最小电压值不是连续区间");
            }
        }
        for (int i = 0, length = maxVolB.length; i < length; i++) {
            BatteryModelDetail bMDetail = new BatteryModelDetail();
            bMDetail.setBatteryModelId(batteryModel.getBatteryModelId());
            bMDetail.setMinVol(Float.valueOf(minVolB[i]));
            bMDetail.setMaxVol(Float.valueOf(maxVolB[i]));
            bMDetail.setBatteryPer(Integer.valueOf(batteryPerB[i]));
            bMDetail.setMilage(Integer.valueOf(milageB[i]));
            batteryModelDetails.add(bMDetail);
        }

        batteryModelDetailDao.insertList(batteryModelDetails);
    }

    /**
     * @description: 根据车辆Id获取车辆绑定的电池方案详细
     * @author: mo.shanyong
     * @date: 2025/4/21 14:42
     * @param: machineId 车辆Id
     * @return: java.util.List<com.tbit.uqbike.object.pojo.BatteryModelDetail>
     */
    @Override
    public List<BatteryModelDetail> getByMachineId(Integer machineId) {
        return batteryModelDetailDao.getListByMachineId(machineId);
    }
}